<template>
  <div
    class="w-full flex items-center"
    :class="[
      'mt-' + (info.extension.topMargin && info.extension.topMargin?.value),
      'mr-' + (info.extension.rightMargin && info.extension.rightMargin?.value),
      'mb-' + (info.extension.bottomMargin && info.extension.bottomMargin?.value),
      'ml-' + (info.extension.leftMargin && info.extension.leftMargin?.value),
    ]"
    :style="{
      height: info.extension.strokeWidth?.value + 'px',
    }"
  >
    <div
      class="relative w-full"
      :style="{
        borderRadius: info.extension.progressRadius?.value + 'px',
        backgroundColor: info.extension.bgColor?.value,
        height: info.extension.strokeWidth?.value + 'px',
      }"
    >
      <div
        class="h-full"
        :style="{
          background:
            info.extension.color?.value.length > 1
              ? `linear-gradient(${info.extension.progressDir?.value},${info.extension.color?.value.map((item) => item.v).join(',')})`
              : info.extension.color?.value[0]?.v,
          width: width * 100 + '%',
          borderRadius: info.extension.progressRadius?.value + 'px',
          textAlign: info.extension.textPosition?.value,
          fontSize: info.extension.fontSize?.value + 'px',
          lineHeight: info.extension.strokeWidth?.value + 'px',
          padding: '0 5px 0 0',
          color: info.extension.textColor?.value,
          fontWeight: info.extension.titleFontWeight?.value,
          transition: 'width .5s',
        }"
      >
        {{ (width * 100).toFixed(info.extension.pointNum?.value) + '%' }}
      </div>
    </div>
    <div
      v-if="info.extension.contentShow?.value"
      :style="{
        fontSize: info.extension.contentFontSize?.value + 'px',
        color: info.extension.contentFontColor?.value,
        fontWeight: info.extension.contentFontWeight?.value,
        margin: '0 5px',
      }"
    >
      {{ progressNum }}
    </div>
  </div>
</template>
<script>
  import { defineComponent } from 'vue'
  import { UiMixin, RefsInjectMixin, ChartMixin } from '@ve/mixin'
  import cloneDeep from 'lodash/cloneDeep'
  export default defineComponent({
    name: 'UiProgressCard',
    mixins: [UiMixin, RefsInjectMixin, ChartMixin],
    inject: {
      hasPreview: { default: false },
    },
    props: {
      info: {
        required: true,
        type: Object,
        default: function () {
          return {}
        },
      },
    },
    data() {
      return {
        infoComponent: {},
        inter: null,
        text: '',
        progressTotal: 1,
        progressNum: 0,
        width: 0,
      }
    },
    watch: {
      info: {
        handler() {
          this.$nextTick(() => {
            this.infoComponent = cloneDeep(this.info)
            this.getRequestData()
            this.inter && (clearInterval(this.inter), (this.inter = null))
            if (this.info.extension.requestSet?.value.loopRequest) {
              this.inter = setInterval(() => {
                this.getRequestData()
              }, this.info.extension.requestSet?.value.loopTime)
            }
          })
        },
        deep: true,
      },
      set: {
        handler(val) {
          console.log(val, '-------')
          this.$forceUpdate()
        },
        deep: true,
      },
      // getSelectModule: {
      //   handler(val, oldVal) {
      //     if (!oldVal.extension) {
      //       if (pieOption[this.info.chartTypeValue]) {
      //         this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = false)
      //       } else {
      //         this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = true)
      //       }
      //     }
      //   },
      //   deep: true,
      // },
    },
    created() {
      this.infoComponent = cloneDeep(this.info)
    },
    mounted() {
      console.log(this.info, '-----------------')
      this.$nextTick(() => {
        this.getRequestData()
        if (this.info.extension.requestSet?.value.loopRequest) {
          this.inter = setInterval(() => {
            this.getRequestData()
          }, this.info.extension.requestSet?.value.loopTime)
        } else {
          this.inter && (clearInterval(this.inter), (this.inter = null))
        }
      })
    },
    deactivated() {
      console.warn('退出----------------------')
      this.inter && clearInterval(this.inter)
    },
    beforeUnmount() {
      this.inter && clearInterval(this.inter)
    },
    methods: {
      getRequestData() {
        this.$request(this.infoComponent.extension?.requestSet?.value, (res) => {
          this.progressTotal = cloneDeep(res)[0][this.infoComponent.extension.requestSet?.value.progressTotal]
          this.progressNum = cloneDeep(res)[0][this.infoComponent.extension.requestSet?.value.progressNum]
          this.width =
            cloneDeep(res)[0][this.infoComponent.extension.requestSet?.value.progressTotal] > 0
              ? cloneDeep(res)[0][this.infoComponent.extension.requestSet?.value.progressNum] /
                cloneDeep(res)[0][this.infoComponent.extension.requestSet?.value.progressTotal]
              : 0
        })
      },
    },
  })
</script>
<style>
  .uiProgressCard {
    width: 100%;
  }
</style>
